package com.zhangyongyi.carhome.adapter.recommend.evaluating;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.zhangyongyi.carhome.R;
import com.zhangyongyi.carhome.entity.greendao.NewesListEntity;
import com.zhangyongyi.carhome.tools.MyOnClickListener;

import java.util.List;

/**
 * Created by dllo on 16/1/13.
 */
public class EvaluatingRecyclerViewAdapter extends RecyclerView.Adapter<EvaluatingRecyclerViewAdapter.MyViewHolder> {
    private List<NewesListEntity> newsLists;
    private RequestQueue queue;
    private LruCache<String, Bitmap> lruCache;
    private ImageCache imageCache;
    private ImageLoader imageLoader;

    //*----*
    private MyOnClickListener myOnClickListener;

    public void setMyOnClickListener(MyOnClickListener myOnClickListener) {
        this.myOnClickListener = myOnClickListener;
    }

    public EvaluatingRecyclerViewAdapter(Context context) {
        queue = Volley.newRequestQueue(context);
        lruCache = new LruCache<String, Bitmap>(20);    //*
        imageCache = new ImageCache() {

            public void putBitmap(String url, Bitmap bitmap) {
                lruCache.put(url, bitmap);
            }


            @Override
            public Bitmap getBitmap(String url) {
                return lruCache.get(url);
            }
        };
        imageLoader = new ImageLoader(queue, imageCache);

    }

    public void addData(List<NewesListEntity> datas) {
        newsLists = datas;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View view = inflater.inflate(R.layout.item_recommend_evaluating, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (newsLists != null && newsLists.size() > 0) {
            final NewesListEntity listEntity = newsLists.get(position);
            holder.title.setText(listEntity.getTitle());
            holder.time.setText(listEntity.getTime());
            holder.replycount.setText(String.valueOf(listEntity.getReplycount()));
            if (listEntity.getSmallpic() != null && listEntity.getSmallpic().length() > 0) {
                String imgUrl = listEntity.getSmallpic();
                //网络拉取图片并显示
                getBitmapCache(imgUrl, holder.imgIv);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myOnClickListener.MyOnClickListener(listEntity.getId());
                }
            });
        }
    }

    public void getBitmapCache(String url, ImageView imageView) {
        //* 构建图片监听器
        ImageListener listener = ImageLoader.getImageListener(
                imageView, R.mipmap.nissan,
                R.mipmap.nissan);
        //* 图片拉取
        imageLoader.get(url, listener);    //*
    }

    @Override
    public int getItemCount() {
        return newsLists != null && newsLists.size() > 0 ? newsLists.size() : 0;
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgIv;
        private TextView title, time, replycount;

        public MyViewHolder(View itemView) {
            super(itemView);
            imgIv = (ImageView) itemView.findViewById(R.id.iv_evaluating_item);
            title = (TextView) itemView.findViewById(R.id.tv_title_item_evaluating);
            time = (TextView) itemView.findViewById(R.id.tv_time_item_evaluating);
            replycount = (TextView) itemView.findViewById(R.id.tv_replyCount_item_evaluating);
        }
    }
}
